package com.system.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class UserDto implements UserDetails, Serializable {

    /**
    * id
    */
    private String id;

    /**
    * 登陆名
    */
    @Length(max =50 ,message = "长度不能超过50位")
    @NotEmpty(message = "登陆名必填")
    private String loginName;

    /**
    * 昵称
    */
    @Length(max =50 ,message = "长度不能超过50位")
    private String name;

    /**
    * 密码
    */
    @NotEmpty(message = "密码必填")
    private String password;
    /**
     * 验证码
     */
    private String imageCode;

    private Integer status;

    private String roleId;

    private String token;
    /**
     * 所有资源，用于前端界面控制
     */
    private List<ResourceDto> resources;

    /**
     * 所有资源中的请求，用于后端接口拦截
     */
    private List<? extends GrantedAuthority> authorities= new ArrayList<>();

    /**
     * 图片验证码token
     */
    private String imageCodeToken;

    public String getImageCode() {
        return imageCode;
    }

    public void setImageCode(String imageCode) {
        this.imageCode = imageCode;
    }

    public String getImageCodeToken() {
        return imageCodeToken;
    }

    public void setImageCodeToken(String imageCodeToken) {
        this.imageCodeToken = imageCodeToken;
    }

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getLoginName() {
    return loginName;
    }

    public void setLoginName(String loginName) {
    this.loginName = loginName;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return loginName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setAuthorities(HashSet<String> request) {
        List<GrantedAuthority> collect=new ArrayList<>();
           if(!CollectionUtils.isEmpty(request)) {
               collect = request.stream().map(e -> {
                   GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(e);
                   return grantedAuthority;
               }).collect(Collectors.toList());
           }
        this.authorities = collect;
    }

    public List<ResourceDto> getResources() {
        return resources;
    }

    public void setResources(List<ResourceDto> resources) {
        this.resources = resources;
    }

    @Override
    public String toString() {
        return "UserDto{" +
                "id='" + id + '\'' +
                ", loginName='" + loginName + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", imageCode='" + imageCode + '\'' +
                ", roleId='" + roleId + '\'' +
                ", imageCodeToken='" + imageCodeToken + '\'' +
                '}';
    }
}